package com.linkhub.oms.admin.modules.pickuporder.util;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.IdUtil;
import com.linkhub.oms.admin.modules.pickuporder.domain.param.EntryOrderLineParam;
import com.linkhub.oms.admin.modules.pickuporder.domain.param.PickUpOrderSaveOrUpdateParam;
import com.linkhub.oms.common.enums.IEnumConvert;
import com.linkhub.oms.common.enums.mp.EntryOrderDeliveryMethodEnum;
import com.linkhub.oms.common.enums.mp.PickUpOrderStatusEnum;
import com.linkhub.oms.common.util.QtStreamUtil;
import com.linkhub.oms.component.mp.util.QtMpUpdateAssist;
import com.linkhub.oms.entity.EntryOrderLineEntity;
import com.linkhub.oms.entity.PickUpGoodsOrderEntity;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;

/**
 * @Description: PickUpOrderAssist
 * @Author: echo
 * @Date: 2024/5/3 15:55
 * @Version: 1.0
 */
public class PickUpOrderAssist {

    public static PickUpGoodsOrderEntity createSaveEntity(PickUpOrderSaveOrUpdateParam saveOrUpdateParam) {

        PickUpGoodsOrderEntity pickUpGoodsOrderEntity = new PickUpGoodsOrderEntity();
        pickUpGoodsOrderEntity.setSysEntryOrderCode(IdUtil.getSnowflake().nextIdStr());
        pickUpGoodsOrderEntity.setUserId(saveOrUpdateParam.getUserId());
        pickUpGoodsOrderEntity.setSysSupplierId(saveOrUpdateParam.getSysSupplierId());
        pickUpGoodsOrderEntity.setDeliveryMethod(IEnumConvert.valueOf(saveOrUpdateParam.getDeliveryMethod(),
                EntryOrderDeliveryMethodEnum.class));

        pickUpGoodsOrderEntity.setAttachmentName(saveOrUpdateParam.getAttachmentName());
        pickUpGoodsOrderEntity.setAttachmentOssKey(saveOrUpdateParam.getAttachmentUrl());

        pickUpGoodsOrderEntity.setLogisticsCompany(saveOrUpdateParam.getLogisticsCompany());
        pickUpGoodsOrderEntity.setLogisticsOddNumber(saveOrUpdateParam.getLogisticsOddNumber());

        pickUpGoodsOrderEntity.setTakeDeliveryCost(saveOrUpdateParam.getTakeDeliveryCost());
        pickUpGoodsOrderEntity.setRealTakeDeliveryCost(saveOrUpdateParam.getRealTakeDeliveryCost());
        pickUpGoodsOrderEntity.setDriverName(saveOrUpdateParam.getDriverName());
        pickUpGoodsOrderEntity.setDriverPhone(saveOrUpdateParam.getDriverPhone());
        pickUpGoodsOrderEntity.setLicensePlate(saveOrUpdateParam.getLicensePlate());
        pickUpGoodsOrderEntity.setTypeOfCar(saveOrUpdateParam.getTypeOfCar());
        pickUpGoodsOrderEntity.setState(PickUpOrderStatusEnum.PENDING_CHECK);
        if (StringUtils.isNotBlank(saveOrUpdateParam.getPredictArrivalTime())) {
            pickUpGoodsOrderEntity.setPredictArrivalTime(
                    LocalDateTimeUtil
                            .parseDate(saveOrUpdateParam.getPredictArrivalTime(), DatePattern.NORM_DATE_FORMATTER)
                            .atStartOfDay()
            );
        }

        String predictDeliveryTime = saveOrUpdateParam.getPredictDeliveryTime();
        if (StringUtils.isNotBlank(predictDeliveryTime)) {
            LocalDateTime predictDeliveryDateTime = LocalDateTimeUtil.parseDate(predictDeliveryTime,
                            DatePattern.NORM_DATE_FORMATTER)
                    .atStartOfDay();
            pickUpGoodsOrderEntity.setPredictDeliveryTime(predictDeliveryDateTime);
        }

        pickUpGoodsOrderEntity.setOrderLines(QtStreamUtil.map(saveOrUpdateParam.getOrderLines(),
                param -> convertOrderLine(pickUpGoodsOrderEntity.getSysEntryOrderCode(), param)));

        return pickUpGoodsOrderEntity;

    }


    public static PickUpGoodsOrderEntity createUpdateEntity(PickUpOrderSaveOrUpdateParam saveOrUpdateParam) {

        PickUpGoodsOrderEntity pickUpGoodsOrderEntity = new PickUpGoodsOrderEntity();
        pickUpGoodsOrderEntity.setSysEntryOrderCode(saveOrUpdateParam.getSysEntryOrderCode());
        pickUpGoodsOrderEntity.setDeliveryMethod(IEnumConvert.valueOf(saveOrUpdateParam.getDeliveryMethod(),
                EntryOrderDeliveryMethodEnum.class));
        pickUpGoodsOrderEntity.setSysSupplierId(saveOrUpdateParam.getSysSupplierId());

        pickUpGoodsOrderEntity.setAttachmentName(saveOrUpdateParam.getAttachmentName());
        pickUpGoodsOrderEntity.setAttachmentOssKey(saveOrUpdateParam.getAttachmentUrl());

        String predictDeliveryTime = saveOrUpdateParam.getPredictDeliveryTime();
        pickUpGoodsOrderEntity.setPredictDeliveryTime(QtMpUpdateAssist.convertDateTimeForUpdate(predictDeliveryTime));

        String predictArrivalTime = saveOrUpdateParam.getPredictArrivalTime();
        pickUpGoodsOrderEntity.setPredictArrivalTime(QtMpUpdateAssist.convertDateTimeForUpdate(predictArrivalTime));

        pickUpGoodsOrderEntity.setLogisticsCompany(saveOrUpdateParam.getLogisticsCompany());
        pickUpGoodsOrderEntity.setLogisticsOddNumber(saveOrUpdateParam.getLogisticsOddNumber());
        pickUpGoodsOrderEntity.setTakeDeliveryCost(saveOrUpdateParam.getTakeDeliveryCost());
        pickUpGoodsOrderEntity.setRealTakeDeliveryCost(saveOrUpdateParam.getRealTakeDeliveryCost());
        pickUpGoodsOrderEntity.setDriverName(saveOrUpdateParam.getDriverName());
        pickUpGoodsOrderEntity.setDriverPhone(saveOrUpdateParam.getDriverPhone());
        pickUpGoodsOrderEntity.setLicensePlate(saveOrUpdateParam.getLicensePlate());
        pickUpGoodsOrderEntity.setTypeOfCar(saveOrUpdateParam.getTypeOfCar());

        pickUpGoodsOrderEntity.setOrderLines(QtStreamUtil.map(saveOrUpdateParam.getOrderLines(),
                param -> convertOrderLine(saveOrUpdateParam.getSysEntryOrderCode(), param)));

        return pickUpGoodsOrderEntity;

    }

    public static EntryOrderLineEntity convertOrderLine(String sysEntryOrderCode, EntryOrderLineParam orderLineParam) {
        EntryOrderLineEntity orderLine = new EntryOrderLineEntity();
        orderLine.setSysEntryOrderCode(sysEntryOrderCode);
        orderLine.setSkuId(orderLineParam.getSkuId());
        orderLine.setSkuQuantity(orderLineParam.getSkuQuantity());

        return orderLine;

    }





}
